package dao.Impl;

import dao.Userdao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.query.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import pojo.TbUser;
import utils.HibernateSessionFactory;

import java.io.Serializable;
import java.util.List;

public class UserdaoImpl extends HibernateDaoSupport implements Userdao {

    @Override
    public TbUser login(TbUser tbUser) {
        try {
            String hql = "from TbUser where userName = ?0 and userPassword = ?1";
            List<TbUser> objects = (List<TbUser>) this.getHibernateTemplate().find(hql, tbUser.getUserName(), tbUser.getUserPassword());
            if (objects == null || objects.size() == 0)
                return null;
            return objects.get(0);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public boolean register(TbUser tbUser) {

        int save = (int)this.getHibernateTemplate().save(tbUser);
        if(save >0)
                return true;
        return false;
    }

}
